home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / SciTex / examples / phong / tex1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  11.4 KB  |  454 lines

  1. /******************************************************************************
  2.  *  tex1.c
  3.  ******************************************************************************
  4.  *
  5.  *  Purpose:
  6.  *      Descriptional text.
  7.  *
  8.  *  Authors:
  9.  *      Michael Teschner and Christian Henn
  10.  *
  11.  *  Note:
  12.  *      None.
  13.  *  
  14.  *  Revisions:
  15.  *      10.11.93    micha    Created file.
  16.  *
  17.  ******************************************************************************
  18.  *
  19.  *  COPYRIGHT (C)                  1992, 1993, 1994
  20.  *
  21.  *  BY  CHRISTIAN HENN             M.E. MUELLER-INSTITUTE FOR MICROSCOPY (MIM)
  22.  *      HENN@COMP.BIOZ.UNIBAS.CH   CH-4056 BASEL, SWITZERLAND   
  23.  *
  24.  *  AND MICHAEL WALDHERR-TESCHNER  SILICON GRAPHICS INDUSTRIES (SGI)
  25.  *      MICHA@BASEL.SGI.COM        CH-4125 RIEHEN, SWITZERLAND
  26.  *
  27.  ******************************************************************************
  28.  *
  29.  *  PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS DOCU-
  30.  *  MENTATION FOR THE PURPOSE OF RESEARCH, DEVELOPMENT AND EDUCATION IS HEREBY
  31.  *  GRANTED FREE OF CHARGE, SUBJECT TO THE FOLLOWING RESTRICTIONS:
  32.  *
  33.  *  THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  34.  *  IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF DESIGN,
  35.  *  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR ARISING FROM A
  36.  *  COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  37.  *
  38.  *  IN NO EVENT SHALL SILICON GRAPHICS OR THE M.E. MUELLER-INSTITUTE BE LIABLE
  39.  *  FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  40.  *  OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  41.  *  WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  42.  *  LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  43.  *  THIS SOFTWARE.
  44.  *
  45.  ******************************************************************************
  46.  */
  47. /*
  48.  *    sptexture -
  49.  *        Stapuft comatible texture mapping support
  50.  *            
  51.  *                Paul Haeberli - 1989            
  52.  */
  53. #include "stdio.h"
  54. #include "gl.h"
  55. #include "math.h"
  56.  
  57. unsigned long *longimagedata();
  58.  
  59. static expandrow();
  60. static setalpha();
  61. static copybw();
  62. static interleaverow();
  63.  
  64. /*
  65.  *    generic texture stuff
  66.  *
  67.  */
  68. typedef struct vect {
  69.     float x, y, z, w;
  70. } vect;
  71.  
  72.  
  73. /*
  74.  *    real texture mapping functions 
  75.  *
  76.  *
  77.  */
  78. static int firsted;
  79. static int texon;
  80.  
  81. static float texps1[] = {
  82.      TX_MINFILTER,TX_POINT,
  83.      TX_MAGFILTER,TX_POINT,
  84.      TX_NULL
  85.      };
  86.  
  87. static float texps2[] = {
  88.      TX_MINFILTER,TX_MIPMAP_BILINEAR,
  89.      TX_MAGFILTER,TX_BILINEAR,
  90.      TX_NULL
  91.      };
  92.  
  93. int high_tex;
  94. unsigned long img1[ 80*256];
  95.  
  96. sbr_tread(name, texno)
  97. char *name;
  98. int texno;
  99. {
  100.     unsigned long *imagedata;
  101.     unsigned long l1,l2,l3,l4;
  102.     int i, xsize, ysize;
  103.     int ix, count;
  104.  
  105.     static float tevps[] = {TV_MODULATE,TV_NULL};
  106.  
  107.  
  108.     sizeofimage(name,&xsize,&ysize);
  109.     printf(" size of image %d x %d y \n",xsize,ysize );
  110.  
  111.     imagedata = (unsigned long *)longimagedata(name);
  112.  
  113.     /* generate a one-component texture out of the RGB image */
  114.     /* this would also work with a 3-component texture */
  115.  
  116.     count = 0;
  117.     for( ix = 0; ix < 256*256; ix +=4){
  118.        l1 = ( imagedata[ix]   & 0x000000ff ) << 24;
  119.        l2 = ( imagedata[ix+1] & 0x000000ff ) << 16;
  120.        l3 = ( imagedata[ix+2] & 0x000000ff ) << 8;
  121.        l4 = ( imagedata[ix+3] & 0x000000ff ) ;
  122.        img1[count++] = l1 + l2 + l3 + l4;
  123.        }
  124.  
  125.     texdef2d(1,1,256,256,img1,0,texps1);
  126.     texdef2d(2,1,256,256,img1,0,texps2);
  127.  
  128.     tevdef(1,0,tevps);
  129.     /* sbr_settexture(1); */
  130.  
  131. }
  132.  
  133. sbr_settexture(n)
  134. int n;
  135. {
  136.     if(n==0) {
  137.         texbind(0,0);
  138.         tevbind(0,0);
  139.     texon = 0;
  140.     } else {
  141.         if( high_tex ) texbind(0,2);
  142.         else           texbind(0,1);
  143.     if(texon == 0) {
  144.             tevbind(0,1);
  145.             texon = 1;
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151. /*******************************************************/
  152.  
  153. /* from fastimage.c */
  154.  
  155. #include <gl/image.h>
  156.  
  157. #define TAGLEN        (5)
  158. /*
  159.  *      sizeofimage -
  160.  *              return the xsize and ysize of an iris image file.
  161.  *
  162.  */
  163. sizeofimage(name, xsize, ysize)
  164. char *name;
  165. int *xsize, *ysize;
  166. {
  167.     IMAGE image;
  168.     FILE *inf;
  169.  
  170.     inf = fopen(name,"r");
  171.     if(!inf) {
  172.         fprintf(stderr,"sizeofimage: can't open image file %s\n",name);
  173.         exit(1);
  174.     }
  175.     fread(&image,sizeof(IMAGE),1,inf);
  176.     if(image.imagic != IMAGIC) {
  177.         fprintf(stderr,"sizeofimage: bad magic number in image file\n");
  178.         exit(1);
  179.     }
  180.     *xsize = image.xsize;
  181.     *ysize = image.ysize;
  182.     fclose(inf);
  183. }
  184.  
  185. /*
  186.  *      longimagedata -
  187.  *              read img a B/W RGB or RGBA iris image file and return a
  188.  *      pointer to an array of longs.
  189.  *
  190.  */
  191. unsigned long *longimagedata(name)
  192. char *name;
  193. {
  194.     unsigned long *base, *lptr;
  195.     unsigned char *rledat, *verdat;
  196.     long *starttab, *lengthtab;
  197.     FILE *inf;
  198.     IMAGE *image;
  199.     int y, z, pos, len, tablen;
  200.     int xsize, ysize, zsize;
  201.     int bpp, rle, cur, badorder;
  202.     int rlebuflen;
  203.  
  204.     inf = fopen(name,"r");
  205.     if(!inf) {
  206.         fprintf(stderr,"longimagedata: can't open image file %s\n",name);
  207.         exit(1);
  208.     }
  209.     image = (IMAGE *)malloc(sizeof(IMAGE));
  210.     fread(image,sizeof(IMAGE),1,inf);
  211.     if(image->imagic != IMAGIC) {
  212.         fprintf(stderr,"longimagedata: bad magic number in image file\n");
  213.         exit(1);
  214.     }
  215.     rle = ISRLE(image->type);
  216.     bpp = BPP(image->type);
  217.     if(bpp != 1 ) {
  218.         fprintf(stderr,"longimagedata: image must have 1 byte per pix chan\n");
  219.         exit(1);
  220.     }
  221.     xsize = image->xsize;
  222.     ysize = image->ysize;
  223.     zsize = image->zsize;
  224.     if(rle) {
  225.         tablen = ysize*zsize*sizeof(long);
  226.         starttab = (long *)malloc(tablen);
  227.         lengthtab = (long *)malloc(tablen);
  228.         rlebuflen = 1.05*xsize+10;
  229.         rledat = (unsigned char *)malloc(rlebuflen);
  230.         fseek(inf,512,SEEK_SET);
  231.         fread(starttab,tablen,1,inf);
  232.         fread(lengthtab,tablen,1,inf);
  233.  
  234. /* check data order */
  235.         cur = 0;
  236.         badorder = 0;
  237.         for(y=0; y<ysize; y++) {
  238.             for(z=0; z<zsize; z++) {
  239.                 if(starttab[y+z*ysize]<cur) {
  240.                     badorder = 1;
  241.                     break;
  242.                 }
  243.                 cur = starttab[y+z*ysize];
  244.             }
  245.             if(badorder)
  246.                 break;
  247.         }
  248.         fseek(inf,512+2*tablen,SEEK_SET);
  249.         cur = 512+2*tablen;
  250.         base = (unsigned long *)
  251.                 malloc((xsize*ysize+TAGLEN)*sizeof(long));
  252.         addlongimgtag(base,xsize,ysize);
  253.         if(badorder) {
  254.             for(z=0; z<zsize; z++) {
  255.                 lptr = base;
  256.                 for(y=0; y<ysize; y++) {
  257.                     if(cur != starttab[y+z*ysize]) {
  258.                         fseek(inf,starttab[y+z*ysize],SEEK_SET);
  259.                         cur = starttab[y+z*ysize];
  260.                     }
  261.                     if(lengthtab[y+z*ysize]>rlebuflen) {
  262.                         fprintf(stderr,"longimagedata: rlebuf is too small - bad poop\n");
  263.                         exit(1);
  264.                     }
  265.                     fread(rledat,lengthtab[y+z*ysize],1,inf);
  266.                     cur += lengthtab[y+z*ysize];
  267.                     expandrow(lptr,rledat,3-z);
  268.                     lptr += xsize;
  269.                 }
  270.             }
  271.         } else {
  272.             lptr = base;
  273.             for(y=0; y<ysize; y++) {
  274.                 for(z=0; z<zsize; z++) {
  275.                     if(cur != starttab[y+z*ysize]) {
  276.                         fseek(inf,starttab[y+z*ysize],SEEK_SET);
  277.                         cur = starttab[y+z*ysize];
  278.                     }
  279.                     fread(rledat,lengthtab[y+z*ysize],1,inf);
  280.                     cur += lengthtab[y+z*ysize];
  281.                     expandrow(lptr,rledat,3-z);
  282.                 }
  283.                lptr += xsize;
  284.             }
  285.         }
  286.         if(zsize == 3)
  287.             setalpha(base,xsize*ysize);
  288.         else if(zsize<3)
  289.             copybw(base,xsize*ysize);
  290.         fclose(inf);
  291.         free(starttab);
  292.         free(lengthtab);
  293.         free(rledat);
  294.         free(image);
  295.         return base;
  296.     } else {
  297.         base = (unsigned long *)
  298.                 malloc((xsize*ysize+TAGLEN)*sizeof(long));
  299.         addlongimgtag(base,xsize,ysize);
  300.         verdat = (unsigned char *)malloc(xsize);
  301.         fseek(inf,512,SEEK_SET);
  302.         for(z=0; z<zsize; z++) {
  303.             lptr = base;
  304.             for(y=0; y<ysize; y++) {
  305.                 fread(verdat,xsize,1,inf);
  306.                 interleaverow(lptr,verdat,3-z,xsize);
  307.                 lptr += xsize;
  308.             }
  309.         }
  310.         if(zsize == 3)
  311.             setalpha(base,xsize*ysize);
  312.         else if(zsize<3)
  313.             copybw(base,xsize*ysize);
  314.         fclose(inf);
  315.         free(verdat);
  316.         free(image);
  317.         return base;
  318.     }
  319. }
  320.  
  321.  
  322.  
  323. /* static utility functions for longimagedata */
  324.  
  325. static interleaverow(lptr,cptr,z,n)
  326. unsigned char *lptr, *cptr;
  327. int z, n;
  328. {
  329.     lptr += z;
  330.     while(n--) {
  331.         *lptr = *cptr++;
  332.         lptr += 4;
  333.     }
  334. }
  335.  
  336. static copybw(lptr,n)
  337. long *lptr;
  338. int n;
  339. {
  340.     while(n>=8) {
  341.         lptr[0] = 0xff000000+(0x010101*(lptr[0]&0xff));
  342.         lptr[1] = 0xff000000+(0x010101*(lptr[1]&0xff));
  343.         lptr[2] = 0xff000000+(0x010101*(lptr[2]&0xff));
  344.         lptr[3] = 0xff000000+(0x010101*(lptr[3]&0xff));
  345.         lptr[4] = 0xff000000+(0x010101*(lptr[4]&0xff));
  346.         lptr[5] = 0xff000000+(0x010101*(lptr[5]&0xff));
  347.         lptr[6] = 0xff000000+(0x010101*(lptr[6]&0xff));
  348.         lptr[7] = 0xff000000+(0x010101*(lptr[7]&0xff));
  349.         lptr += 8;
  350.         n-=8;
  351.     }
  352.     while(n--) {
  353.         *lptr = 0xff000000+(0x010101*(*lptr&0xff));
  354.         lptr++;
  355.     }
  356. }
  357. static setalpha(lptr,n)
  358. unsigned char *lptr;
  359. {
  360.     while(n>=8) {
  361.         lptr[0*4] = 0xff;
  362.         lptr[1*4] = 0xff;
  363.         lptr[2*4] = 0xff;
  364.         lptr[3*4] = 0xff;
  365.         lptr[4*4] = 0xff;
  366.         lptr[5*4] = 0xff;
  367.         lptr[6*4] = 0xff;
  368.         lptr[7*4] = 0xff;
  369.         lptr += 4*8;
  370.         n -= 8;
  371.     }
  372.     while(n--) {
  373.         *lptr = 0xff;
  374.         lptr += 4;
  375.     }
  376. }
  377. static expandrow(optr,iptr,z)
  378. unsigned char *optr, *iptr;
  379. int z;
  380. {
  381.     unsigned char pixel, count;
  382.  
  383.     optr += z;
  384.     while(1) {
  385.         pixel = *iptr++;
  386.         if ( !(count = (pixel & 0x7f)) )
  387.             return;
  388.         if(pixel & 0x80) {
  389.             while(count>=8) {
  390.                 optr[0*4] = iptr[0];
  391.                 optr[1*4] = iptr[1];
  392.                 optr[2*4] = iptr[2];
  393.                 optr[3*4] = iptr[3];
  394.                 optr[4*4] = iptr[4];
  395.                 optr[5*4] = iptr[5];
  396.                 optr[6*4] = iptr[6];
  397.                 optr[7*4] = iptr[7];
  398.                 optr += 8*4;
  399.                 iptr += 8;
  400.                 count -= 8;
  401.             }
  402.             while(count--) {
  403.                 *optr = *iptr++;
  404.                 optr+=4;
  405.             }
  406.         } else {
  407.             pixel = *iptr++;
  408.             while(count>=8) {
  409.                 optr[0*4] = pixel;
  410.                 optr[1*4] = pixel;
  411.                 optr[2*4] = pixel;
  412.                 optr[3*4] = pixel;
  413.                 optr[4*4] = pixel;
  414.                 optr[5*4] = pixel;
  415.                 optr[6*4] = pixel;
  416.                 optr[7*4] = pixel;
  417.                 optr += 8*4;
  418.                 count -= 8;
  419.             }
  420.             while(count--) {
  421.                 *optr = pixel;
  422.                 optr+=4;
  423.             }
  424.         }
  425.     }
  426. }
  427.  
  428. /*
  429.  *      addlongimgtag -
  430.  *              this is used to extract image data from core dumps.
  431.  *
  432.  */
  433. addlongimgtag(dptr,xsize,ysize)
  434. unsigned long *dptr;
  435. int xsize, ysize;
  436. {
  437.     dptr = dptr+(xsize*ysize);
  438.     dptr[0] = 0x12345678;
  439.     dptr[1] = 0x59493333;
  440.     dptr[2] = 0x69434222;
  441.     dptr[3] = xsize;
  442.     dptr[4] = ysize;
  443. }
  444.  
  445.  
  446.  
  447. /*mssize () {}
  448. zbsize () { } 
  449. multisample () {}
  450. t3f () {}
  451. */
  452.  
  453.  
  454.